package com.coldwindblows.blog.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {
	private final static String pattern = "yyyy-MM-dd HH:mm:ss";
	
	public final static String YMD_PATTERN = "yyyy-MM-dd";
	
	public static String toTime(Date date) {
		return toTime(date, pattern);
	}
	
	public static String toTime(Date date, String pattern) {
		if(date == null) return "";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static Date stringToDate(String str) {
		return stringToDate(str, pattern);
	}
	
	public static Date stringToDate(String str, String pattern) {
		if(str == null || str.isEmpty()) return null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = format.parse(str);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 将毫秒值转换成yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String millisecondToTime(String millisecond) {
		if(millisecond == null) return "";
		try{
			long time = Long.parseLong(millisecond);
			Date date = new Date(time);
			String result = toTime(date, pattern);
			return result;
		} catch(Exception e) {
			e.printStackTrace();
			return millisecond;
		}
	}
	
	/**
	 * 判断传入时间是否是今天
	 * @param oldTime
	 * @return
	 */
	public static boolean isToday(Date oldTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String todayStr = format.format(new Date());
		try {
			Date today = format.parse(todayStr);
			if(today.getTime() - oldTime.getTime() <= 0) {
				return true;
			}
			return false;
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * future day zero time
	 * @param date current date
	 * @param span  unit：day
	 * @return
	 */
	public static Date futureDayZeroTime(Date date,int span) {
		return futureZeroTime(date, null, null, span);
	}
	
	/**
	 * future month zero time
	 * @param date current date
	 * @param span  unit：month
	 * @return
	 */
	public static Date futureMonthZeroTime(Date date, int span) {
		return futureZeroTime(date, null, span, null);
	}
	
	public static Date futureMonthTime(Date date, int span) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, span);
		return calendar.getTime();
	}
	
	
	/**
	 * future year zero time
	 * @param date current date
	 * @param span  unit：year
	 * @return
	 */
	public static Date futureYearZeroTime(Date date, int span) {
		return futureZeroTime(date, span, null, null);
	}
	
	private static Date futureZeroTime(Date date,Integer yearSpan, Integer monthSpan, Integer daySpan) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
		//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
		if(daySpan != null) {
			calendar.add(Calendar.DATE, daySpan);
		}
		//把日期往前减少一个月，若想把日期向后推一个月则将负数改为正数
		if(monthSpan != null) {
			calendar.add(Calendar.MONTH, monthSpan);
		}
		//把日期往前减少一年，若想把日期向后推一年则将负数改为正数
		if(yearSpan != null) {
			calendar.add(Calendar.YEAR, yearSpan);
		}
		return calendar.getTime();
	}

}
